//
//  LLGoodsDetailBottomView.m
//  LoveLove
//
//  Created by 肖兰月 on 2019/4/7.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLGoodsDetailBottomView.h"

@interface LLGoodsDetailBottomView ()
@property (strong, nonatomic) LLButton *contactBtn;
@property (strong, nonatomic) LLButton *shoppingCartBtn;
@property (strong, nonatomic) LLButton *buyBtn;
@property (strong, nonatomic) LLButton *leftSwitchBtn;
@property (strong, nonatomic) LLButton *rightSwitchBtn;
@property (strong, nonatomic) CALayer *switchLayer;

@property (assign, nonatomic) LLGoodsDetailBottomViewType type;
@end

@implementation LLGoodsDetailBottomView

- (instancetype)initWithFrame:(CGRect)frame type:(LLGoodsDetailBottomViewType)type {
    self = [super initWithFrame:frame];
    if (self) {
        self.type = type;
        [self initSubViews];
    }
    return self;
}

- (void)initSubViews {
    CALayer *separateLine = CALayer.layer;
    separateLine.frame = CGRectMake(0, 0, self.width, 0.6);
    separateLine.backgroundColor = LLColorRGB(67).CGColor;
    [self.layer addSublayer:separateLine];
    
    _contactBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"contact" titleColor:nil font:0];
    _contactBtn.tag = 0;
    _contactBtn.frame = CGRectMake(20, (self.height-34)/2.0, 34, 34);
    [_contactBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_contactBtn];
    
    _shoppingCartBtn = [LLInitBaseViewTool buttonWithTitle:nil image:@"shoppingCart_grey" titleColor:nil font:0];
    _shoppingCartBtn.tag = 1;
    _shoppingCartBtn.frame = CGRectMake(_contactBtn.right+15, (self.height-34)/2.0, 34, 34);
    [_shoppingCartBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_shoppingCartBtn];
    
    if (self.type == LLGoodsDetailBottomViewTypeSwitch) {
        CGFloat bgViewW = LLScreenWidth < 375 ? 200 : 220;
        
        CALayer *bgView = CALayer.layer;
        bgView.frame = CGRectMake(self.width-bgViewW-10, (self.height-46)/2.0, bgViewW, 46);
        bgView.cornerRadius = 23;
        bgView.masksToBounds = YES;
        bgView.backgroundColor = LLColor(245, 179, 177).CGColor;
        [self.layer addSublayer:bgView];
        
        _switchLayer = CALayer.layer;
        _switchLayer.frame = CGRectMake(self.width-bgViewW-10, (self.height-46)/2.0, bgViewW/2 + 6, 46);
        _switchLayer.cornerRadius = 23;
        _switchLayer.masksToBounds = YES;
        _switchLayer.backgroundColor = LLColor(225, 0, 0).CGColor;
        [self.layer addSublayer:_switchLayer];
        
        _leftSwitchBtn = [LLInitBaseViewTool buttonWithTitle:@"¥199\n单独购买" image:nil titleColor:UIColor.whiteColor font:16];
        _leftSwitchBtn.tag = 2;
        _leftSwitchBtn.frame = CGRectMake(self.width-bgViewW-10, (self.height-46)/2.0, bgViewW/2.0, 46);
        [_leftSwitchBtn.layer addMaskWithRoundedRect:_leftSwitchBtn.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(23, 23)];
        _leftSwitchBtn.titleLabel.numberOfLines = 2;
        _leftSwitchBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_leftSwitchBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftSwitchBtn];
        
        _rightSwitchBtn = [LLInitBaseViewTool buttonWithTitle:@"¥99\n发起拼团" image:nil titleColor:UIColor.whiteColor font:16];
        _rightSwitchBtn.tag = 3;
        _rightSwitchBtn.frame = CGRectMake(_leftSwitchBtn.right, (self.height-46)/2.0, bgViewW/2.0, 46);
        [_rightSwitchBtn.layer addMaskWithRoundedRect:_rightSwitchBtn.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(23, 23)];
        _rightSwitchBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _rightSwitchBtn.titleLabel.numberOfLines = 2;
        [_rightSwitchBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightSwitchBtn];
    } else {
        _buyBtn = [LLInitBaseViewTool buttonWithTitle:nil image:nil titleColor:UIColor.whiteColor font:0];
        _buyBtn.tag = 4;
        _buyBtn.backgroundColor = LLColor(225, 0, 0);
        _buyBtn.frame = CGRectMake(self.width-180-10, (self.height-46)/2.0, 180, 46);
        _buyBtn.layer.cornerRadius = 23;
        [_buyBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_buyBtn];
    }
}

- (void)btnClick:(LLButton *)btn {
    if (btn.tag == 2 || btn.tag == 3) {
        CGFloat bgViewW = LLScreenWidth < 375 ? 200 : 220;
        CGFloat scrollLeft = btn.tag == 2 ? _leftSwitchBtn.left : LLScreenWidth-(bgViewW/2+6)-10;
        [UIView animateWithDuration:0.1 animations:^{
            self.switchLayer.left = scrollLeft;
        }];
    } else {
        if ([self.delegate respondsToSelector:@selector(goodsDetailBottomViewDidClickButtonWithIndex:)]) {
            [self.delegate goodsDetailBottomViewDidClickButtonWithIndex:btn.tag];
        }
    }
}


@end
